﻿Imports System.Collections.Specialized
Imports System.IO
Imports System.Globalization
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Collections.ObjectModel
Imports System

Imports System.ComponentModel

Imports System.Xml.Serialization

Imports System.Threading
Imports Infragistics.Windows.Ribbon

Namespace Controlers

    Public Enum ControlerStates
        None
        PendingDependancy
        Loading
        Ready
    End Enum

    Public MustInherit Class ControlerBase(Of TView As {IView, UIElement})
        Implements INotifyPropertyChanged, IDisposable


        Private m_View As TView

        Public ReadOnly Property View() As TView
            Get
                Return m_View
            End Get
        End Property

#Region " INotifyPropertyChanged "

        Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

        Public Sub OnPropertyChanged(ByVal PropertyName As String)
            If Not View.Dispatcher.CheckAccess Then
                View.Dispatcher.Invoke(New Action(Of String)(AddressOf OnPropertyChanged), PropertyName)
            End If
            If Not PropertyChangedEvent Is Nothing Then
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
                PropertyHasChanged(PropertyName)
            End If
        End Sub

        Protected MustOverride Sub PropertyHasChanged(ByVal PropertyName As String)


#End Region

        Private m_Working As ControlerStates = ControlerStates.None

        Public Property Working() As ControlerStates
            Get
                Return m_Working
            End Get
            Protected Set(ByVal value As ControlerStates)
                If Not value = m_Working Then
                    m_Working = value
                    OnPropertyChanged("Working")
                End If
            End Set
        End Property

        Private m_State As ControlerStates = ControlerStates.None

        Protected MustOverride Sub RecalculateState()

        Public Property State() As ControlerStates
            Get
                Return m_State
            End Get
            Protected Set(ByVal value As ControlerStates)
                If Not value = m_State Then
                    m_State = value
                    OnPropertyChanged("State")
                End If
            End Set
        End Property

        Private m_SuperControler As ControlerFactory(Of TView)

        Public Sub New(ByVal view As IView)
            m_View = view

            Initilise()
            InitiliseObjects()
            State = ControlerStates.Loading
            InitiliseLoad()
            InitiliseNavigation()
        End Sub

        Public Property Factory() As ControlerFactory(Of TView)
            Get
                Return m_SuperControler
            End Get
            Friend Set(ByVal value As ControlerFactory(Of TView))
                m_SuperControler = value
            End Set
        End Property

        Public MustOverride Sub Reset()

        Protected MustOverride Sub Initilise()
        Protected MustOverride Sub InitiliseObjects()
        Protected MustOverride Sub InitiliseLoad()
        Protected MustOverride Sub InitiliseNavigation()

#Region " Ribbon "

        Public Function GetRibbonTabItem(ByVal Name As String, ByVal Header As String) As RibbonTabItem
            Dim tab As RibbonTabItem = View.Ribbon.Tabs.SingleOrDefault(Function(t) (t.Name = Name))
            If tab Is Nothing Then
                tab = New RibbonTabItem
                tab.Header = Header
                tab.Name = Name
                View.Ribbon.Tabs.Add(tab)
            End If
            Return tab
        End Function

        Public Function GetRibbonTabItem(ByVal ContextualTabGroup As ContextualTabGroup, ByVal Name As String, ByVal Header As String) As RibbonTabItem
            Dim tab As RibbonTabItem = ContextualTabGroup.Tabs.SingleOrDefault(Function(t) (t.Name = Name))
            If tab Is Nothing Then
                tab = New RibbonTabItem
                tab.Header = Header
                tab.Name = Name
                ContextualTabGroup.Tabs.Add(tab)
            End If
            Return tab
        End Function

        Public Function GetContextualTabGroup(ByVal Key As String, ByVal Caption As String) As ContextualTabGroup
            Dim tabGroup As ContextualTabGroup = View.Ribbon.ContextualTabGroups.SingleOrDefault(Function(t) (t.Key = Key))
            If tabGroup Is Nothing Then
                tabGroup = New ContextualTabGroup
                tabGroup.Caption = Caption
                tabGroup.Key = Key
                tabGroup.IsVisible = False
                View.Ribbon.ContextualTabGroups.Add(tabGroup)
            End If
            Return tabGroup
        End Function


        Public Function GetRibbonGroup(ByVal ribbonTabItem As RibbonTabItem, ByVal Name As String, ByVal Caption As String) As RibbonGroup
            Dim rg As RibbonGroup = ribbonTabItem.RibbonGroups.SingleOrDefault(Function(t) (t.Name = Name))
            If rg Is Nothing Then
                rg = New RibbonGroup
                rg.Caption = Caption
                rg.Name = Name
                ribbonTabItem.RibbonGroups.Add(rg)
                Dim newToolHorizontalWrapPanel As New ToolHorizontalWrapPanel
                rg.Items.Add(newToolHorizontalWrapPanel)
            End If
            Return rg
        End Function

        Protected Sub RibbonGroupAddVariant(ByVal ribbonGroup As RibbonGroup, ByVal Priority As Integer, ByVal ResizeAction As GroupVariantResizeAction)
            Dim newGroupVariant As New GroupVariant
            newGroupVariant.Priority = Priority
            newGroupVariant.ResizeAction = ResizeAction
            If Not ribbonGroup.Variants.Contains(newGroupVariant) Then
                ribbonGroup.Variants.Add(newGroupVariant)
            End If
        End Sub

#End Region

        'Get Image Source
        Protected Function GetImageSource(ByVal uriString As String) As BitmapImage
            Dim bmpImage As New BitmapImage
            bmpImage.BeginInit()
            bmpImage.UriSource = New Uri(uriString, UriKind.RelativeOrAbsolute)
            bmpImage.EndInit()
            Return bmpImage
        End Function

#Region " IDisposable "

        Private disposedValue As Boolean = False        ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: free other state (managed objects).
                    DisposeManagedObjects()
                End If

                ' TODO: free your own state (unmanaged objects).
                ' TODO: set large fields to null.
                DisposeUnmanagedObjects()
            End If
            Me.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

        Protected MustOverride Sub DisposeManagedObjects()
        Protected MustOverride Sub DisposeUnmanagedObjects()

#End Region

    

    End Class

End Namespace